package cs.gui;


import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JProgressBar;
//import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;

/**
 * First window to open to the user. It asks the user for the username
 * and wait the clic on connect button. <p>
 * Package  cs.gui
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 * @version 1.0
 */
@SuppressWarnings("serial")
public class WelcomeFrame extends  JFrame  {

	/**
	 * Text Field to entering the username of the local user
	 */
	private JPanel contentPane;

	/**
	 * Field containing the username, changed by the user
	 */
	private JTextField fieldUsername;

	/**
	 * Button connect
	 */
	private JButton btnConnect; 

	/**
	 * Label error
	 */
	private JLabel lblError, lblError2 ; 

	
	/**
	 * Create the frame.
	 */
	public WelcomeFrame() {
		initGUI();
		setVisible(true);

	}

	/**
	 * Construct and initiate the frame components. 
	 */
	private void initGUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		fieldUsername = new JTextField();
		
		fieldUsername.addKeyListener (new KeyAdapter() 
		{
			@Override
			public void keyPressed(KeyEvent evt) {
				int key = evt.getKeyCode();  
				if (key == KeyEvent.VK_ENTER)  
					fieldUsername.transferFocus();  
			}  
		}); 


		fieldUsername.setColumns(10);

		btnConnect = new JButton("Se connecter");

		JLabel lblPseudo = new JLabel("Nom d'utilisateur :");

		JLabel lblChatsystemG = new JLabel("ChatSystem G1");
		lblChatsystemG.setFont(new Font("Lucida Grande", Font.BOLD, 20));
		
		lblError = new JLabel("La connexion au chat est impossible,");
		lblError.setForeground(Color.RED);
		
		lblError2 = new JLabel("veuillez v\u00E9rifier le port utilis\u00E9 et votre connexion r\u00E9seau.");
		lblError2.setForeground(Color.RED);
		lblError.setForeground(Color.RED);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(44)
					.addComponent(lblChatsystemG)
					.addContainerGap(241, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(163)
					.addComponent(btnConnect, GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
					.addGap(153))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(207, Short.MAX_VALUE)
					.addComponent(lblError, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
					.addGap(93))
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(53)
					.addComponent(lblPseudo)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(fieldUsername, GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
					.addGap(122))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(48, Short.MAX_VALUE)
					.addComponent(lblError2)
					.addGap(37))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(46)
					.addComponent(lblChatsystemG)
					.addGap(48)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(fieldUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPseudo))
					.addGap(18)
					.addComponent(btnConnect)
					.addGap(18)
					.addComponent(lblError)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblError2)
					.addContainerGap(18, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		btnConnect.setEnabled(false);

		this.getRootPane().setDefaultButton(btnConnect);
		lblError.setVisible(false); 
		lblError2.setVisible(false); 


	}

	/**
	 * 
	 * @return fieldUsername the username writed by user (String)
	 */
	public JTextField getUsernameField() {
		return fieldUsername;
	}

	/**
	 * @return btnConnect the button ordering the connection in chat
	 */
	public JButton getConnectButton() {
		return btnConnect;
	}

	
	/**
	 * Used to inform to local user that the network don't works
	 * @param error true if an error is reached, false if the network is ok
	 */
	public void informError(Boolean error) {
			lblError.setVisible(error); 
			lblError2.setVisible(error); 
	}
}
